// 引入配置
import config from '@/utils/config'
// 加密解密
import { encrypt, decrypt } from '@/utils/encrypt.js'
const request = function request(url, data, method,header1) {
    var _url = config.ApiUrl + config.subDomain + url;

    const currentDate = new Date()
    const month = currentDate.getMonth() + 1
    const datetime = `${currentDate.getFullYear()}-${month.toString().padStart(2, '0')}-${currentDate.getDate().toString().padStart(2, '0')} ${currentDate.getHours().toString().padStart(2, '0')}:${currentDate.getMinutes().toString().padStart(2, '0')}:${currentDate.getSeconds().toString().padStart(2, '0')}`
    const encryptedDateTime = encrypt(datetime)

    var header = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'openid': uni.getStorageSync('openid') || '',
        'Lang': uni.getStorageSync('lang') || 'zh',
        "Ok": encryptedDateTime,
        ...header1
    };
    return new Promise(function (resolve, reject) {
        uni.request({
            url: _url,
            method: method,
            data: data,
            header: header,
            success: function success(request) {
                resolve(request.data);
            },
            fail: function fail(error) {
                reject(error);
            },
            complete: function complete(aaa) {
                // 加载完成
            }
        });
    });
};

function api(url, data, type = 'get',header) {
    return request(url, data, type,header)
}

export default api
